package com.shmn.gongjiajumain.service.impl;

import com.shmn.gongjiajumain.entity.RepairSubmitEntity;
import com.shmn.gongjiajumain.service.UserService;
import com.shmn.gongjiajumain.utils.DateUtil;
import com.shmn.gongjiajumain.vo.outgoing.OutGoingJigFormVo;
import com.shmn.gongjiajumain.vo.outgoing.OutGoingJigTableVo;
import com.shmn.gongjiajumain.vo.repair.RepairSubmitHistoryTableVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shmn.common.utils.PageUtils;
import com.shmn.common.utils.Query;

import com.shmn.gongjiajumain.dao.OutgoingJigDao;
import com.shmn.gongjiajumain.entity.OutgoingJigEntity;
import com.shmn.gongjiajumain.service.OutgoingJigService;


@Service("outgoingJigService")
public class OutgoingJigServiceImpl extends ServiceImpl<OutgoingJigDao, OutgoingJigEntity> implements OutgoingJigService {

    @Autowired
    private UserService userService;

    @Override
    public PageUtils queryPage1(OutGoingJigFormVo outGoingJigFormVo) {
        /**
         * 条件查询+分页
         */
        Map<String, Object> params = new HashMap<>();
        params.put("page",outGoingJigFormVo.getPageIndex());
        params.put("limit",outGoingJigFormVo.getPageSize());
        params.put("key",outGoingJigFormVo.getKey());
        IPage<OutgoingJigEntity> page = this.page(
                new Query<OutgoingJigEntity>().getPage(params),
                new QueryWrapper<OutgoingJigEntity>()
        );

        /**
         * 构建并返回vo对象
         */
        PageUtils pageUtils = new PageUtils(page);
        List<OutGoingJigTableVo> outGoingJigTableVos = new ArrayList<>();
        Class<OutgoingJigEntity> clazz = OutgoingJigEntity.class;
        pageUtils.getList().forEach(item->{
            OutGoingJigTableVo outGoingJigTableVo = new OutGoingJigTableVo();
            BeanUtils.copyProperties(item,outGoingJigTableVo);
            String userId = null;
            Date outcomeTime = null;
            try {
                Field field = clazz.getDeclaredField("userId");
                Field field1 = clazz.getDeclaredField("outcomeTime");
                field.setAccessible(true);
                field1.setAccessible(true);
                userId = (String) field.get(item);
                outcomeTime = (Date) field1.get(item);
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
            outGoingJigTableVo.setUserName(userService.getById(userId).getName());
            outGoingJigTableVo.setOutcomeTime(DateUtil.formatDate(outcomeTime));
            outGoingJigTableVo.setCodeName("超级无敌至尊工夹具");
            outGoingJigTableVos.add(outGoingJigTableVo);
        });
        pageUtils.setList(outGoingJigTableVos);
        return pageUtils;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OutgoingJigEntity> page = this.page(
                new Query<OutgoingJigEntity>().getPage(params),
                new QueryWrapper<OutgoingJigEntity>()
        );

        return new PageUtils(page);
    }

}